package com.xs.ahoi.hcxs.ui.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.adapter.MySimpleAdapter;
import com.xs.ahoi.hcxs.dao.BookDao;
import com.xs.ahoi.hcxs.model.vo.BatchBookItemVO;
import com.xs.ahoi.hcxs.utils.FileUtil;
import com.xs.ahoi.hcxs.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 书籍批量管理
 */
public class BookBatchActivity extends BaseActivity {

    @BindView(R.id.lvBooks)
    public ListView lvBooks;
    @BindView(R.id.tvDeleteOnly)
    public TextView tvDeleteOnly;
    @BindView(R.id.tvDeleteLocal)
    public TextView tvDeleteLocal;
    @BindView(R.id.ivDeleteLocal)
    public ImageView ivDeleteLocal;
    @BindView(R.id.tvSelectAll)
    public TextView tvSelectAll;
    // 未选中/已选中底色
    private int itemSelectedBgColor, itemUnSelectedBgColor;
    private BookDao bookDao;
    // listview数据集
    private BatchAdapter adapter;
    private List<BatchBookItemVO> datas;
    private List<BatchBookItemVO> selectedDatas;
    // 右上角删除按钮复选框
    private Drawable drawableUncheck, drawableChecked;
    private boolean isDeleteLocal = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_batch);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        bookDao = BookDao.getInstance();

        // 右上角删除按钮复选框
        drawableUncheck = ContextCompat.getDrawable(this, R.drawable.icon_rect_uncheck);
        drawableChecked = ContextCompat.getDrawable(this, R.drawable.icon_rect_check);

        // 未选中图标， 已选中图标
        itemSelectedBgColor = ContextCompat.getColor(this, R.color.colorDivider);
        itemUnSelectedBgColor = ContextCompat.getColor(this, R.color.colorWhite);
        selectedDatas = new ArrayList<>();

        // 绑定到listview
        bindListView();
    }

    // 绑定到listview
    private void bindListView() {
        datas = bookDao.listAll().stream()
                .map(BatchBookItemVO::convertFromBook)
                .collect(Collectors.toList());

        adapter = new BatchAdapter(this, datas, R.layout.listview_item_batch);
        lvBooks.setAdapter(adapter);

        // 刷新当前页面下方按钮文字
        selectedDatas.clear();
        updateView();
    }

    @OnClick({R.id.deleteLocal, R.id.tvDeleteOnly, R.id.tvSelectAll, R.id.arrowLeft})
    public void onClick(View v) {
        switch (v.getId()) {
            // 同时删除本地文件
            case R.id.deleteLocal:
                if (isDeleteLocal) {
                    ivDeleteLocal.setImageDrawable(drawableUncheck);
                    tvDeleteLocal.setTextColor(ContextCompat.getColor(this, R.color.colorWhite));
                } else {
                    ivDeleteLocal.setImageDrawable(drawableChecked);
                    tvDeleteLocal.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
                }
                isDeleteLocal = !isDeleteLocal;
                break;
            // 删除
            case R.id.tvDeleteOnly:
                if (selectedDatas.isEmpty()) {
                    ToastUtil.show("您还没有选择");
                } else {
                    // 从数据库删除
                    List<String> pathList = selectedDatas.stream().map(BatchBookItemVO::getPath).collect(Collectors.toList());
                    bookDao.deleteByPath(pathList);

                    // 删除本地文件
                    if (isDeleteLocal) {
                        FileUtil.delete(pathList);
                    }
                    ToastUtil.show("已刪除");

                    // 更新页面
                    bindListView();
                }
                break;
            // 全选
            case R.id.tvSelectAll:
                if (datas.isEmpty()) {
                    ToastUtil.show("没有可选项");
                } else {
                    if (selectedDatas.size() < datas.size()) {
                        selectedDatas.clear();
                        selectedDatas.addAll(datas);
                    } else {
                        selectedDatas.clear();
                    }
                    updateView();
                }
                break;
            // 左上角返回箭头
            case R.id.arrowLeft:
                super.onBackPressed();
                break;
            default:
                break;
        }
    }

    @OnItemClick({R.id.lvBooks})
    public void onItemClick(int position) {

        BatchBookItemVO item = datas.get(position);

        if (selectedDatas.contains(item)) {
            selectedDatas.remove(item);
        } else {
            selectedDatas.add(item);
        }
        updateView();

        // 更新删除按钮的已选数量
        if (selectedDatas.size() > 0) {
            tvDeleteOnly.setText(getString(R.string.delete) + "(" + selectedDatas.size() + ")");
        } else {
            tvDeleteOnly.setText(getString(R.string.delete));
        }
    }

    // 更新listview和删除按钮
    private void updateView() {
        adapter.notifyDataSetChanged();
        if (selectedDatas.size() < datas.size() || datas.size() == 0) {
            tvSelectAll.setText(getString(R.string.select_all));
            tvDeleteOnly.setText(getString(R.string.delete));
        } else {
            tvSelectAll.setText(getString(R.string.cancel));
            tvDeleteOnly.setText(getString(R.string.delete) + "(" + selectedDatas.size() + ")");
        }
    }

    // 自定义BatchAdapter
    private class BatchAdapter extends MySimpleAdapter<BatchBookItemVO> {

        private BatchAdapter(Context context, List<BatchBookItemVO> datas, int resource) {
            super(context, datas, resource);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = super.getView(position, convertView, parent);
            BatchBookItemVO item = getItem(position);

            // 设置书名
            TextView tvBookName = view.findViewById(R.id.tvBookName);
            tvBookName.setText(item.getBookName());

            // 设置大小
            TextView tvBookInfo = view.findViewById(R.id.tvBookInfo);
            tvBookInfo.setText(item.getBookSize());

            // 设置进度
            TextView tvPercent = view.findViewById(R.id.tvPercent);
            tvPercent.setText(item.getPercent());

            // 设置选中效果
            if (selectedDatas.contains(item)) {
                view.setBackgroundColor(itemSelectedBgColor);
            } else {
                view.setBackgroundColor(itemUnSelectedBgColor);
            }
            return view;
        }
    }

}
